﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MyOA.BLL;
using MyOA.Model;
using MyOA.DAL;

namespace MyOA.UI.SysManage
{
    public partial class LoginLog : System.Web.UI.Page
    {
        LoginLogManage manage = new LoginLogManage();
        private int Pager
        {
            set { ViewState["page"] = value; }
            get { return (int)ViewState["page"]; }
        }
        private DateTime StartTime
        {
            set { ViewState["start"] = value; }
            get { return (DateTime)ViewState["start"]; }
        }
        private DateTime EndTime
        {
            set { ViewState["end"] = value; }
            get { return (DateTime)ViewState["end"]; }
        }
        public void dataBind(IList<MyOA.Model.LoginLog> loginLogs)
        {
            rpLoginLog.DataSource = loginLogs;
            rpLoginLog.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["loginId"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                    return;

                }
                if (Request.QueryString["NodeValuePath"] == null)
                {
                    Response.Redirect("~/Error.html");
                    return;
                }
                int nodeId;
                if (int.TryParse(Request.QueryString["NodeValuePath"], out nodeId) == false)
                {
                    Response.Redirect("~/Error.html");
                    return;
                }
                string loginId = (string)Session["loginId"];
                if (new CommandManage().isHaveRoleRight(loginId, nodeId) == false)
                {
                    Response.Redirect("~/Error.html");
                    return;
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            StartTime = UserCalender1.StartTime;
            EndTime = UserCalender1.EndTime;
            Pager = 1;
            int sum;
            IList<MyOA.Model.LoginLog> loginLogs = manage.getLoginLogByLoginTime(StartTime, EndTime, 5, Pager, 0, out sum);
            AspNetPager1.RecordCount = sum;
            if (sum > 0)
            {
                dataBind(loginLogs);
            }




        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Pager = AspNetPager1.CurrentPageIndex;
            int sum;

            IList<MyOA.Model.LoginLog> loginLogs = manage.getLoginLogByLoginTime(StartTime, EndTime, 5, Pager, 1, out sum);

            dataBind(loginLogs);

        }

        /// <summary>
        /// repeater控件里的子控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkbtnDelete_Click(object sender, EventArgs e)
        {
            //确保最后一页有数据
            if (Pager > 1)
            {
                double pageCount = AspNetPager1.RecordCount / 5;
                pageCount = Math.Ceiling(pageCount);
                if ((pageCount + 1) == Pager)
                {
                    Pager -= 1;
                }
                AspNetPager1.CurrentPageIndex = Pager;
            }
            int loginLogId = int.Parse(((LinkButton)sender).CommandName);
            if (manage.deleteLoginLogByLoginLogId(loginLogId))
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('删除成功',null,null,'温馨提示')</script>", false);
                int sum;
                IList<MyOA.Model.LoginLog> loginLogs = manage.getLoginLogByLoginTime(StartTime, EndTime, 5, Pager, 0, out sum);
                AspNetPager1.RecordCount = sum;
                if (sum > 0)
                {
                    dataBind(loginLogs);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('删除失败',null,null,'温馨提示')</script>", false);
            }

        }

        /// <summary>
        /// repeater控件里的子控件 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkbtnAllDelete_Click(object sender, EventArgs e)
        {
            if (txtIds.Value != string.Empty)
            {
                string ids = txtIds.Value;
                ids = ids.Substring(0, ids.LastIndexOf(","));

                if (manage.deleteLoginLogByLoginLogId(ids))
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('删除成功',null,null,'温馨提示')</script>", false);

                    //确保最后一页有数据
                    if (Pager > 1)
                    {
                        double pageCount = AspNetPager1.RecordCount / 5;
                        pageCount = Math.Ceiling(pageCount);
                        if ((pageCount + 1) == Pager)
                        {
                            Pager -= 1;
                        }
                        AspNetPager1.CurrentPageIndex = Pager;
                    }

                    int sum;
                    IList<MyOA.Model.LoginLog> loginLogs = manage.getLoginLogByLoginTime(StartTime, EndTime, 5, Pager, 0, out sum);
                    AspNetPager1.RecordCount = sum;
                    if (sum > 0)
                    {
                        dataBind(loginLogs);
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('删除失败',null,null,'温馨提示')</script>", false);
                }
            }


        }
    }
}
